package cn.stylefeng.guns.modular.customer.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.customer.entity.Rebate;
import cn.stylefeng.guns.modular.customer.mapper.RebateMapper;
import  cn.stylefeng.guns.modular.customer.service.RebateService;
import cn.stylefeng.guns.modular.customer.wrapper.RebateWarpper;
import cn.stylefeng.guns.modular.customer.wrapper.UserWarpper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分销记录表 服务实现类
 * </p>
 *
 * @author typ
 * @since 2021-04-25
 */
@Service
public class RebateServiceImpl extends ServiceImpl<RebateMapper, Rebate> implements RebateService {

    @Override
    public void add(Rebate param){
        Rebate entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(Rebate param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(Rebate param){
        Rebate oldEntity = getOldEntity(param);
        Rebate newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public Rebate findBySpec(Rebate param){
        return null;
    }

    @Override
    public List<Rebate> findListBySpec(Rebate param){

        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(Rebate param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageMapList(pageContext, param);
        Page<Map<String, Object>> wrap = new RebateWarpper((Page<Map<String, Object>>) page).wrap();
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(Rebate param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Rebate getOldEntity(Rebate param) {
        return this.getById(getKey(param));
    }

    private Rebate getEntity(Rebate param) {
        Rebate entity = new Rebate();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
